﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Twodi.Input.Events;
using Twodi.Interfaces;
using Microsoft.Xna.Framework;
using Twodi.Behavior.Interfaces;
using Twodi.Input;

namespace Twodi.Behavior.Implemented
{
    public class KeyboardBehavior : AbstractBehavior<IKeyboardBehavior>, IKeyboardEvents
    {
        protected List<KeyboardEvent> keyboardEvents;

        public KeyboardBehavior(IKeyboardBehavior component)
            : base(component)
        {
            keyboardEvents = new List<KeyboardEvent>();
        }

        public override bool Handle(KeyboardInput input)
        {
            if (!Enabled) return true;

            for (int i = 0; i < Events.Count; i++)
            {
                if (!Events[i].Handle(input)) 
                {
                    return false;
                }                
            }

            return true;
        }

        public override bool Handle(MouseInput input)
        {
            throw new NotImplementedException();
        }

        public override void Initialize()
        {
            return;
        }

        public override void Update(GameTime gameTime)
        {
            return;
        }

        public override void Draw(GameTime gameTime)
        {
            return;
        }

        public List<KeyboardEvent> Events
        {
            get { return this.keyboardEvents; }
            set
            {
                this.keyboardEvents = value;
                onKeyboardEventsChanged(EventArgs.Empty);
            }
        }

        public event EventHandler<EventArgs> KeyboardEventsChanged;

        protected virtual void onKeyboardEventsChanged(EventArgs e)
        {
            if (KeyboardEventsChanged != null)
            {
                KeyboardEventsChanged(this, e);
            }
        }
    }
}
